package cn.xfdzx.android.apps.shop.util;

import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.PopupWindow;

import cn.xfdzx.android.apps.shop.R;

/**
 * @author gzk
 * @date 2020/4/10 0010
 * description：
 */
public class ShopCarPopupWindow extends PopupWindow {
    private Activity mContext;
    private View mView;
    private View mRootView;
    public ShopCarPopupWindow(Activity cx, int layout) {
        this.mContext = cx;
        mView = LayoutInflater.from(mContext).inflate(layout, null);
        setContentView(mView);
        mRootView = mView.findViewById(R.id.sv_popup_root);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shareClose();
            }
        });
    }
    public void showPopupWindow() {
        initAnims();
        showAtLocation(mContext.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
        mRootView.setAnimation(animShow);
    }

    public void shareClose() {
        animHide.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                ShopCarPopupWindow.this.dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        mRootView.clearAnimation();
        mRootView.setAnimation(animHide);
    }

    public View getRootView() {
        return mView;
    }

    /**
     * 显示时的动画
     */
    private Animation animShow;
    /**
     * 隐藏时的动画
     */
    private Animation animHide;

    private void initAnims() {
        animShow = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 1,
                Animation.RELATIVE_TO_SELF, 0);
        animShow.setDuration(300);

        animHide = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 1);
        animHide.setDuration(300);
    }
}
